<?php  
session_start();  
include './logincheck.php';  
nologin();  
include 'configs/config.php';  

$message = "";  
$article_id = $_GET['id'] ?? null; // 获取文章 ID  

if (!$article_id) {  
    header("Location: my_articles.php?message=文章ID缺失");  // 重定向到我的文章页面  
    exit();  
}  

// 查询当前文章  
$sql = "SELECT * FROM article WHERE id = ?";  
$stmt = $conn->prepare($sql);  
$stmt->bind_param("i", $article_id);  
$stmt->execute();  
$result = $stmt->get_result();  

if ($result->num_rows > 0) {  
    $article = $result->fetch_assoc();  
    
    // 检查是否为文章作者  
    if ($article['user_id'] !== $_SESSION['user_id']) {  
        header("Location: my_articles.php?message=您没有权限编辑此文章");  // 重定向到我的文章页面  
        exit();  
    }  
} else {  
    header("Location: my_articles.php?message=文章不存在");  // 重定向到我的文章页面  
    exit();  
}  

if ($_SERVER["REQUEST_METHOD"] == "POST") {  
    $title = $_POST['title'];  
    $body = $_POST['body'];  
    $visibility = $_POST['visibility']; // 更新隐私设置  

    $sql = "UPDATE article SET title = ?, body = ?, visibility = ? WHERE id = ?";  
    $stmt = $conn->prepare($sql);  
    $stmt->bind_param("sssi", $title, $body, $visibility, $article_id);  

    if ($stmt->execute()) {  
        header("Location: my_articles.php?message=文章修改成功");  // 修改成功后重定向  
        exit();  
    } else {  
        $message = "修改文章失败: " . $stmt->error;  
    }  
    $stmt->close();  
}  
?>  

<!DOCTYPE html>  
<html lang="zh">  
<head>  
    <meta charset="UTF-8">  
    <title>修改文章</title>  
    <link rel="stylesheet" href="static/style.css">  

    <style>  
        /* 与 publish.php 中的样式相同 */  
        body {  
            font-family: 'Arial', sans-serif;  
            background-color: #f9f9f9;  
            color: #333;  
            display: flex;  
            flex-direction: column;  
            align-items: center;  
            justify-content: center;  
            height: 100vh;  
            margin: 0;  
            padding: 20px;  
        }  

        h1 {  
            color: #4A90E2;  
            margin-bottom: 20px;  
            text-align: center;  
        }  

        form {  
            background-color: #fff;  
            padding: 20px;  
            border-radius: 10px;  
            box-shadow: 0 4px 10px rgba(0, 0, 0, 0.1);  
            width: 100%;  
            max-width: 600px;  
            display: flex;  
            flex-direction: column;  
            align-items: center;  
        }  

        input[type="text"], textarea {  
            width: 100%;  
            padding: 10px;  
            margin: 10px 0;  
            border: 1px solid #ccc;  
            border-radius: 5px;  
            font-size: 1em;  
            transition: border-color 0.3s;  
        }  

        input[type="text"]:focus, textarea:focus {  
            border-color: #4A90E2;  
            outline: none;  
        }  

        textarea {  
            height: 200px;  
            resize: none;  
        }  

        input[type="submit"] {  
            background-color: #4A90E2;  
            color: white;  
            border: none;  
            padding: 10px 15px;  
            border-radius: 5px;  
            cursor: pointer;  
            font-size: 1em;  
            transition: background-color 0.3s;  
        }  

        input[type="submit"]:hover {  
            background-color: #E94E77;  
        }  

        .message {  
            margin-top: 20px;  
            font-size: 1.2em;  
            color: #333;  
            text-align: center;  
        }  

        a {  
            margin-top: 20px;  
            text-decoration: none;  
            color: #4A90E2;  
            font-size: 1em;  
        }  

        a:hover {  
            text-decoration: underline;  
        }  
    </style>  
</head>  
<body>  
    <h1>修改文章</h1>  
    <form method="post">  
        <input type="text" name="title" placeholder="标题" value="<?php echo htmlspecialchars($article['title']); ?>" required>  
        <textarea name="body" placeholder="内容" required><?php echo htmlspecialchars($article['body']); ?></textarea>  
        
        <label for="visibility">隐私设置：</label>  
        <select name="visibility" required>  
            <option value="公开" <?php echo $article['visibility'] === '公开' ? 'selected' : ''; ?>>公开</option>  
            <option value="私密" <?php echo $article['visibility'] === '私密' ? 'selected' : ''; ?>>私密</option> 
            <option value="部分公开" <?php echo $article['visibility'] === '部分公开' ? 'selected' : ''; ?>>部分公开</option> 
        </select>  

        <input type="submit" value="修改">  
    </form>  
    <div class="message"><?php echo htmlspecialchars($message); ?></div>  
    <a href="index.php">返回主页</a>  
</body>  
</html>